package com.ysq;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.function.BiConsumer;

/**
 * map的增删找查
 */
public class MapDemo {
    public static void main(String[] args) {
        test3();
    }

    public static void test3() {
        Map<String, Double> map = new HashMap<>();
        map.put("ysq",150.0);
        map.put("zq",120.0);
        map.put("zxy",11.0);
        map.put("mmc",50.0);

        map.forEach(new BiConsumer<String, Double>() {
            @Override
            public void accept(String key, Double value) {
                System.out.println(key + "---" + value);
            }
        });

        System.out.println("---------------------");

        //lambda表达式
        map.forEach((key, value) -> System.out.println(key + "---" + value));
    }

        public static void test2() {
        Map<String, Double> map = new HashMap<>();
        map.put("ysq",150.0);
        map.put("zq",120.0);
        map.put("zxy",11.0);
        map.put("mmc",50.0);
        //1.将map集合转成set集合（将map中的key-value绑定成一个对象entry
        Set<Map.Entry<String, Double>> entrySet = map.entrySet();
        //2.遍历set集合
        for (Map.Entry<String, Double> entry : entrySet) {
            //3.根据entry获取key和value
            String key= entry.getKey();
            Double value = entry.getValue();
            System.out.println(key + "---" + value);
        }
    }


    public static void test1() {
            Map<String, Double> map = new HashMap<>();
            map.put("ysq",150.0);
            map.put("zq",120.0);
            map.put("zxy",11.0);
            map.put("mmc",50.0);
           //将map中的所有键取出来放到set集合中；
        Set <String> KeySet = map.keySet();
        for (String key : KeySet) {
            //根据Key去找值
           Double value =  map.get(key);
           System.out.println(key + "---" + value);
        }
    }

    private static void test0() {
        Map<String, Double> map = new HashMap<>();
        map.put("ysq",150.0);
        map.put("zq",120.0);
        map.put("zxy",11.0);
        map.put("mmc",50.0);
        System.out.println(map);

        map.remove("mmc");
        System.out.println(map);
        System.out.println("----------");
//        map.clear();
//        System.out.println(map);

        boolean b1= map.containsKey("zxy");
        System.out.println("b1 = "+b1);
        boolean b2 = map.containsValue(120.0);
        System.out.println("b2 = "+b2);

        boolean empty = map.isEmpty();
        System.out.println("empty = "+empty);
        int size = map.size();
        System.out.println("size = "+size);
    }
}
